package book.config;

import book.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理:
 * 创建类用@ControllerAdvice注解修饰,方法用@ExceptionHandler注解修饰
 */
@Slf4j
@ControllerAdvice
@ResponseBody //返回为数据类型,加注解@ResponseBody
public class ExceptionAdvice {
    @ExceptionHandler
    public Result handlerException(Exception e){
        log.error("内部异常:e :",e);
        return Result.fild("内部异常!" );
    }
    @ExceptionHandler
    public Result handlerException(NullPointerException e){
        log.error("空指针异常:e :",e);
        return Result.fild("空指针异常!" );
    }
    @ExceptionHandler
    public Result handlerException(IndexOutOfBoundsException e){
        log.error("下标越界异常:e :",e);
        return Result.fild("下标越界异常!" );
    }
    @ExceptionHandler
    public Result handlerException(ArithmeticException e){
        log.error("算数异常:e :",e);
        return Result.fild("算数异常!" );
    }
}
